package top.infopub.mgc.common;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import top.infopub.util.ObjectUtils;
import top.infopub.vo.ResponseVo;


/**
 * dubbo访问日志
 * @author Awoke
 * @version 2018年9月27日
 * @see DubboLogAop
 * @since
 */
@Component
@Aspect
public class DubboLogAop {

    private static final Logger logger = LoggerFactory.getLogger(DubboLogAop.class);

    /**
     * list中的ResponseVo data会被清空 防止log日志过大
     */
    private static List<String> ESCAPE_DATA_LOG_LIST = new ArrayList<>();
    static {
        ESCAPE_DATA_LOG_LIST.add("ResponseVo top.infopub.mgc.wsImpl.WsTemplateBuildServiceImpl.generatorHtml(TemplateBuildVo)");
        ESCAPE_DATA_LOG_LIST.add("ResponseVo top.infopub.mgc.wsImpl.WsTemplateBuildServiceImpl.pdfFileStream(String)");
    }

    @Pointcut("execution (* top.infopub.*.wsImpl.*.*(..)) ")
    public void pointcut() {}

    @Before("pointcut()")
    public void before() {}

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point)
        throws Throwable {
        String _call = point.getSignature().toString();
        logger.info("dubbo-call {}", _call);
        logger.info("params: {}", Arrays.toString(point.getArgs()));
        Object object = point.proceed();
        // 处理log日志 防止打印的日志过大
        if (ESCAPE_DATA_LOG_LIST.contains(_call)) {
            ResponseVo<String> rsp = ResponseVo.getSuccessResponse();
            ObjectUtils.copyObjValue(object, rsp, true);
            rsp.setData(null);
            logger.info("return: {} (dataESCAPE)", rsp.toString());
        }
        else {
            logger.info("return: {}", object == null ? "" : object.toString());
        }
        return object;
    }

}
